package com.bootdo.haojiangboutils;

import com.bootdo.common.utils.StringUtils;
import com.haojiangbo.annotation.HapiFeild;

import java.lang.reflect.Field;

/**
 *
 * 请求参数验证
 　　* @author 郝江波
 　　* @date 2019/9/11 14:25
 　　*/
public class RequestParCheckUtils {

    /*
     验证指定参数是否为空
     */
    public static Object checkPar(Object obj, String ... par){
        if(null == par || null == obj)
            throw new NullPointerException("参数不能为空");

        Class clazz = obj.getClass();
        for(String item : par){
            try {
                Field field =  clazz.getDeclaredField(item);
                field.setAccessible(true);
                Object r =  field.get(obj);
                if(null == r){
                    HapiFeild hapiFeild =   field.getAnnotation(HapiFeild.class);
                    if(null != hapiFeild && StringUtils.isNotEmpty(hapiFeild.value())){
                        return  hapiFeild.value()+"不能为空";
                    }
                    return item+"不能为空";
                }
            } catch (Exception e) {
                e.printStackTrace();
                return "检测异常";
            }
        }

        return  true;
    }


}
